package com.ruoyi.common.core.exception;

import com.ruoyi.common.core.exception.domain.ExceptionMessage;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

/**
 * 基础异常
 *
 * @author ruoyi
 */
public class BaseException extends RuntimeException implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 所属模块
     */
//    private String module;

    /**
     * 错误码
     */
//    private String code;

    /**
     * 错误码对应的参数
     */
//    private Object[] args;

    /**
     * 错误消息
     */
//    private String defaultMessage;

    private ExceptionMessage exceptionMessage;

    public BaseException(ExceptionMessage exceptionMessage) {
        this.exceptionMessage = exceptionMessage;
    }

    public BaseException(String module, String code, Object[] args, String businessMessage) {
        this.exceptionMessage = new ExceptionMessage(module, code, args, businessMessage);
    }

    public BaseException(String module, String code, Object[] args) {
        this(module, code, args, null);
    }

    public BaseException(String module, String businessMessage) {
        this(module, null, null, businessMessage);
    }

    public BaseException(String code, Object[] args) {
        this(null, code, args, null);
    }

    public BaseException(String businessMessage) {
        this(null, null, null, businessMessage);
    }


    public String getModule() {
        return exceptionMessage.getModule();
    }

    public String getCode() {
        return exceptionMessage.getCode();
    }

    public BaseException setCode(String code) {
        exceptionMessage.setCode(code);
        return this;
    }

    public Object[] getArgs() {
        return exceptionMessage.getArgs();
    }

    @Deprecated
    public String getDefaultMessage() {
        return exceptionMessage.getBusinessMessage();
    }

    public String getBusinessMessage() {
        return exceptionMessage.getBusinessMessage();
    }

    public BaseException setBusinessMessage(String businessMessage) {
        exceptionMessage.setBusinessMessage(businessMessage);
        return this;
    }

    public ExceptionMessage getExceptionMessage() {
        return exceptionMessage;
    }

    public ExceptionMessage.LevelEnum getLevelEnum() {
        return exceptionMessage.getLevelEnum();
    }

    public BaseException setLevelEnum(ExceptionMessage.LevelEnum levelEnum) {
        exceptionMessage.setLevelEnum(levelEnum);
        return this;
    }

    public Integer getStatus() {
        return exceptionMessage.getStatus();
    }

    public BaseException setStatus(Integer status) {
        exceptionMessage.setStatus(status);
        return this;
    }

    public Object getData() {
        return exceptionMessage.getData();
    }

    public BaseException setData(Object data) {
        exceptionMessage.setData(data);
        return this;
    }

    @Override
    public String getMessage() {
        return super.getMessage() != null ? super.getMessage() : getBusinessMessage();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
                .append("module", getModule())
                .append("code", getCode())
                .append("args", getArgs())
                .append("businessMessage", getBusinessMessage())
                .append("message", getMessage())
                .append("level", getLevelEnum().name())
                .toString();
    }

}
